import sqlite3
from datetime import datetime
from cash_flow import CashFlowDB

def init_cash_flow_data():
    """初始化现金流水表和示例数据"""
    # 创建表
    db = CashFlowDB()
    
    # 添加示例数据
    sample_data = [
        # USDT充值
        (1, 1, 'deposit', 1000.0, '银行转账', 2.0, '通过银行转账充值USDT'),
        # BTC提现
        (1, 2, 'withdraw', -0.5, '链上地址', 0.01, '提取BTC到个人钱包'),
        # 法币入金
        (1, None, 'fiat_in', 50000.0, '银行转账', 0.0, '人民币入金'),
        # 法币出金
        (1, None, 'fiat_out', -20000.0, '银行转账', 0.0, '人民币出金到银行账户'),
        # ETH充值
        (1, 3, 'deposit', 5.0, '交易所', 0.1, '从其他交易所转入ETH'),
        # USDT提现
        (1, 1, 'withdraw', -200.0, '链上地址', 1.0, '提取USDT到去中心化钱包'),
    ]
    
    try:
        with sqlite3.connect('investment_history.db') as conn:
            cursor = conn.cursor()
            for user_id, asset_id, flow_type, amount, channel, fee, note in sample_data:
                cursor.execute(
                    "INSERT INTO cash_flow (user_id, asset_id, flow_type, amount, channel, fee, note, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
                    (user_id, asset_id, flow_type, amount, channel, fee, note, datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
                )
            conn.commit()
            print(f"成功插入 {len(sample_data)} 条示例数据")
    except sqlite3.Error as e:
        print(f"插入示例数据时发生错误: {e}")

if __name__ == "__main__":
    init_cash_flow_data()